package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.MaterialType;
import com.mt.roll.core.dal.service.MaterialTypeServiceBiz;
import com.mt.roll.core.enums.WhetherEnum;
import com.mt.roll.core.params.EnabledParam;
import com.mt.roll.core.params.IdListParam;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.CodeGeneratorHelper;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.params.MaterialTypeParam;
import com.mt.roll.wms.params.MaterialTypeQueryParam;
import com.mt.roll.wms.service.MaterialTypeService;
import com.mt.roll.wms.vo.MaterialTypeVo;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author weihongyang
 * @Date 2021/1/8 13:43
 * @Version 1.0
 */
@Service
@Transactional
public class MaterialTypeServiceImpl extends BaseService implements MaterialTypeService {
    @Autowired
    private MaterialTypeServiceBiz materialTypeServiceBiz;


    /**
     * 获取xx
     *
     * @param idParam 主键参数
     * @return xx
     */
    @Override
    public R<MaterialTypeVo> get(IdParam idParam) {
        MaterialType materialType = materialTypeServiceBiz.getById(idParam.getId());
        Assert.notNull("维修类型不存在", materialType);
        MaterialTypeVo materialTypeVo = MaterialTypeVo.builder().build();
        BeanUtils.copyProperties(materialType, materialTypeVo);
        return successful(materialTypeVo);
    }

    /**
     * 获取xx列表
     *
     * @param materialTypeQueryParam xx查询参数
     * @return xx列表
     */
    @Override
    public R<List<MaterialTypeVo>> list(MaterialTypeQueryParam materialTypeQueryParam) {
        return null;
    }

    /**
     * 获取xx分页列表
     *
     * @param materialTypeQueryParam xx查询参数
     * @return xx分页列表
     */
    @Override
    public R<PageVo<MaterialTypeVo>> page(MaterialTypeQueryParam materialTypeQueryParam) {
        QueryWrapper<MaterialType> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(materialTypeQueryParam.getEnabled()), MaterialType.ENABLED, materialTypeQueryParam.getEnabled())
                .like(StringUtils.isNotBlank(materialTypeQueryParam.getName()), MaterialType.NAME, materialTypeQueryParam.getName())
                .orderByDesc(MaterialType.CREATE_TIME);
        IPage<MaterialType> page = materialTypeServiceBiz.page(new Page<>(materialTypeQueryParam.getCurrent(), materialTypeQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page, MaterialTypeVo.class));
    }

    /**
     * 获取xx树列表
     *
     * @param materialTypeQueryParam xx查询参数
     * @return xx树列表
     */
    @Override
    public R<List<MaterialTypeVo>> tree(MaterialTypeQueryParam materialTypeQueryParam) {
        return null;
    }

    /**
     * 新增xx
     *
     * @param materialTypeParam xx参数
     * @return 主键
     */
    @Override
    public R<IdVo> add(MaterialTypeParam materialTypeParam) {
        QueryWrapper<MaterialType> wrapper = new QueryWrapper<>();
        wrapper.eq(MaterialType.CODE, materialTypeParam.getCode());
        Assert.eqZero(materialTypeServiceBiz.count(wrapper), "物料类型编码已经存在");
        QueryWrapper<MaterialType> wrapperName = new QueryWrapper<>();
        wrapperName.eq(MaterialType.NAME, materialTypeParam.getName());
        Assert.eqZero(materialTypeServiceBiz.count(wrapperName), "物料名称已经存在");
        MaterialType materialType = new MaterialType();
        BeanUtils.copyProperties(materialTypeParam, materialType);
        setCommonField(materialType);
        materialTypeServiceBiz.save(materialType);
        return successful(IdVo.builder().id(materialType.getId()).build());
    }

    /**
     * 更新xx
     *
     * @param materialTypeParam xx参数
     * @return 主键
     */
    @Override
    public R<IdVo> update(MaterialTypeParam materialTypeParam) {
        MaterialType materialType = materialTypeServiceBiz.getById(materialTypeParam.getId());
        //修改工厂编码需要检验新编码是否已存在
        if (!materialType.getCode().equals(materialTypeParam.getCode())) {
            QueryWrapper<MaterialType> wrapper = new QueryWrapper<>();
            wrapper.eq(MaterialType.CODE, materialTypeParam.getCode());
            Assert.eqZero(materialTypeServiceBiz.count(wrapper), "维修类型编码已经存在");
        }
        MaterialType updateMaterialType = new MaterialType();
        BeanUtils.copyProperties(materialTypeParam, updateMaterialType);
        setUpdateCommonField(updateMaterialType);
        materialTypeServiceBiz.updateById(updateMaterialType);
        return successful(IdVo.builder().id(materialType.getId()).build());
    }

    /**
     * 启用停用
     *
     * @param enabledParam 启停参数
     * @return 主键
     */
    @Override
    public R<IdVo> enabled(EnabledParam enabledParam) {
        return null;
    }

    /**
     * 删除xx
     *
     * @param idParam 主键参数
     * @return 主键
     */
    @Override
    public R<IdVo> delete(IdParam idParam) {
        MaterialType materialType = materialTypeServiceBiz.getById(idParam.getId());
        if (materialType == null) {
            return failed("维修类型不存在");
        }
        //使用更新方式删除，需要把code设置为历史code（后面跟随机数）
        MaterialType updateMaterialType = new MaterialType().setId(materialType.getId());
        updateMaterialType.setCode(materialType.getCode() + "_" + RandomUtils.nextInt());
        updateMaterialType.setValid(WhetherEnum.NO.getValue());
        setUpdateCommonField(updateMaterialType);
        materialTypeServiceBiz.updateById(updateMaterialType);
        //更新时逻辑删除字段没有更新，需要再调用一次删除操作
        materialTypeServiceBiz.removeById(updateMaterialType.getId());
        return successful(IdVo.builder().id(materialType.getId()).build());
    }

    /**
     * 批量删除xx
     *
     * @param idListParam 主键列表参数
     * @return 主键列表
     */
    @Override
    public R<IdListVo> delete(IdListParam idListParam) {
        return null;
    }

    @Override
    public R<String> getCode() {
        return successful(CodeGeneratorHelper.getMaterialTypeCode());
    }
}
